package pers.mine.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import pers.mine.po.GoodsItem;
import pers.mine.po.GoodsItemExample;
import pers.mine.service.inter.GoodsItemService;
import pers.mine.utils.MineScrew;
import pers.mine.vo.Info;
import pers.mine.vo.PageInfo;

/**
 * @author Mine
 * @since 2016年10月21日_下午2:54:37
 */
@Controller
@RequestMapping("/goods")
public class AdminGoodsController {
	private String pageTitle = "商品管理";
	private Info indexinfo = new Info();// 放到首页的信息
	@Autowired
	GoodsItemService goodsItemService;
	String onePath = "admin/goods/";
	String imgPath = "/assets/goodsimg";

	/**
	 * 依据分页信息,关键字信息 获得商品列表,并展示到后台商品页
	 */
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String show(Integer nowPage, Integer pageSize, Integer pageCount,
			String keys, Model model) {
		GoodsItemExample exam = new GoodsItemExample();
		/*
		 * System.out.println("nowPage:"+nowPage);
		 * System.out.println("pageSize:"+pageSize);
		 * System.out.println("keys:"+keys);
		 */
		// 页号非法性验证
		// System.out.println(pageCount);
		// System.out.println(nowPage);
		if (pageCount != null && nowPage != null && pageCount < nowPage) {
			nowPage = pageCount;
		}
		exam.initLimit(nowPage, pageSize);
		if (keys != null && !"".equals(keys.trim())) {
			// 关键字写回页面
			model.addAttribute("keys", keys);

			String keysPro = "%" + keys + "%";
			// 写死了。。。
			exam.or().andNameLike(keysPro);// 商品名
			exam.or().andTypeLike(keysPro);// 类型
			exam.or().andOriginLike(keysPro);// 产地
			exam.or().andDescLike(keysPro);// 简介

		}
		// 排序方式
		exam.setOrderByClause("g_id,g_name");
		// System.out.println(exam.showBase());
		// 依据筛选条件获取
		PageInfo<GoodsItem> pi = goodsItemService.findPageByExample(exam);
		// System.out.println(pi);
		// 判断是否可以放入信息
		if (indexinfo.isCanChange()) {
			indexinfo.setType(Info.SUCCESS);
			if (keys != null && !"".equals(keys.trim())) {
				indexinfo.setMsg("[商品显示]:对于关键字[" + keys + "]查询商品成功...");
			} else {
				indexinfo.setMsg("[商品显示]:查询所有商品成功...");
			}
		} else {
			indexinfo.setCanChange(true);
		}
		model.addAttribute("pageTitle", pageTitle);
		model.addAttribute("indexinfo", indexinfo);
		model.addAttribute("goodsPageInfo", pi);
		return onePath + "goodsPage";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(GoodsItem goodsItem, MultipartFile imgFile,
			HttpServletRequest request, Model model) {
		System.out.println(goodsItem);
		// System.out.println( imgFile.getContentType() );
		// System.out.println( imgFile.getName() );
		// System.out.println( imgFile.getOriginalFilename() );
		if (goodsItem == null || goodsItem.getPrice() == null
				|| goodsItem.getPrice() <= 0) {
			System.out.println("商品信息有误...[这里和数据校验重复了]");
			return "redirect:show";
		}
		String imgInfo = null;
		String path = request.getServletContext().getRealPath(imgPath);
		// 执行上传
		String img = MineScrew.upLoadImgForStringMVC(path, "goods", imgFile);
		// System.out.println(img);
		if (img != null) {
			goodsItem.setImg(img);
			imgInfo = "###商品图片上传成功!";
		} else {
			imgInfo = "###商品图片未指定或上传失败!";
		}
		// 执行添加操作
		if (goodsItemService.addGoods(goodsItem)) {
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[商品上架]:完成商品上架操作..." + imgInfo);
		} else {
			// 添加商品失败,所有要...删除图片
			if (img != null) {
				// 尝试删除旧图片
				File x = new File(new File(path), img);
				System.out.println(x.getAbsolutePath());
				x.delete();
				imgInfo = "###已上传图片已尝试删除!";
			}
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品上架]:商品上架失败{商品名重复?存在其他未填写信息?}..." + imgInfo);
		}
		indexinfo.setCanChange(false);
		// 内部转发
		return "redirect:show";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getEditGoodsInfoPage(@PathVariable("id") Integer id,
			String oper, Model model) {
		if (oper == null || "".equals(oper.trim())) {
			oper = "editGoods";
		}
		model.addAttribute("pageTitle", pageTitle);
		GoodsItem one = null;
		if (id != null && id > 0) {
			// 获取指定id商品信息
			one = goodsItemService.getOneGoodsItemByID(id);
			if (one != null) {
				model.addAttribute("oneGoods", one);
				model.addAttribute("indexinfo", indexinfo);
				indexinfo.setCanChange(true);
				return onePath + oper;// 有多种选择
			}
		}
		indexinfo.setType(Info.ERROR);
		indexinfo.setMsg("[商品信息修改]:非法的物品编号...");
		indexinfo.setCanChange(false);
		return "redirect:show";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String editBaseInfo(GoodsItem goodsItem, Model model) {
		if (goodsItem == null) {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品信息修改]:未知错误...");
			indexinfo.setCanChange(false);
			return "redirect:../show";// 向上返回一个层级
		}
		// 说明无需修改
		if (goodsItem.getState() < 0) {
			goodsItem.setBrand(null);
		}
		// 执行修改操作
		if (goodsItemService.editGoodsInfo(goodsItem)) {// 成功
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[商品信息修改]:修改#" + goodsItem.getId() + "号商品成功...");
			indexinfo.setCanChange(false);
			return "redirect:../show";// 向上返回一个层级
		} else {// 失败
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[商品信息修改]:修改#" + goodsItem.getId()
					+ "号商品失败,重新修改...");
			indexinfo.setCanChange(false);
			return "redirect:" + goodsItem.getId() + "?oper=editGoods";
		}

	}

	@RequestMapping(value = "/editIMG", method = RequestMethod.POST)
	public String editIMG(Integer id, String oldimg, MultipartFile imgFile,
			Model model, HttpServletRequest request) {
		if (id == null || id < 1) {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品图片修改]:未知错误...");
			indexinfo.setCanChange(false);
			return "redirect:show";// 向上返回一个层级
		}

		String imgInfo = null;
		String path = request.getServletContext().getRealPath(imgPath);
		// 执行上传
		String img = MineScrew.upLoadImgForStringMVC(path, "goods", imgFile);
		// System.out.println(img);
		if (img != null) {
			imgInfo = "###商品图片上传成功!";
		} else {
			imgInfo = "###商品图片未指定或上传失败!";
		}

		// 执行修改操作
		if (goodsItemService.editGoodsIMG(id, img)) {
			if (oldimg != null && !"".equals(oldimg.trim())) {
				// 尝试删除旧图片
				new File(new File(path), oldimg).delete();
			}
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[商品图片修改]:完成图片修改操作..." + imgInfo);
			indexinfo.setCanChange(false);
			return "redirect:show";// 向上返回一个层级
		} else {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品图片修改]:商品图片修改失败..." + imgInfo);
			indexinfo.setCanChange(false);
			return "redirect:edit/" + id + "?oper=editIMG";
		}

	}

}
